/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */
package com.cxhd.dc.tran;

import com.cxhd.dc.DataConfig;
import com.cxhd.dc.websocket.Message;
import com.cxhd.dc.websocket.StartServer;
import it.sauronsoftware.ftp4j.FTPDataTransferListener;
import java.io.File;
import java.util.logging.Level;
import java.util.logging.Logger;

/**
 *
 * @author Administrator
 */
public class UploadListener implements FTPDataTransferListener {

    private File uploadFile;
    private int i = 0;
    private int fileSize = 0;
    private boolean stop = true;

    public UploadListener(File uploadFile) {
        this.uploadFile = uploadFile;
        getPro();
    }

    public UploadListener() {
    }

    @Override
    public void started() {
    }

    @Override
    public void transferred(int length) {
        i += length;
        // System.out.println("上传进度：" + i * 1.0 / uploadFile.length() + "  " + i + "   " + uploadFile.length());

    }

    @Override
    public void completed() {
        stop = false;
    }

    @Override
    public void aborted() {
    }

    @Override
    public void failed() {
        stop = false;
    }

    private void getPro() {
        new Thread(new Runnable() {

            @Override
            public void run() {
                while (stop) {
                    try {
                        Thread.sleep(1000);
                        Message msg = new Message();
                        msg.setType("pro");
                        fileSize = new File(DataConfig.getInstance().getBasePath() + "/" + DataConfig.getInstance().getZipFolder()).listFiles().length;
                        msg.setBody(i * 1.0 / uploadFile.length() + "@" + fileSize);
                        StartServer.getInstance().sayToAllMsg(msg);
                    } catch (Exception ex) {
                    }
                }
            }
        }).start();
    }

}
